package logic

import (
	"context"
	"elem_svc/models/mysql"
	"fmt"

	"elem_svc/elem"
	"elem_svc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 注册
func (l *RegisterLogic) Register(in *elem.RegisterRequest) (*elem.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User
	mysql.DB.Debug().Where("account = ?", in.Account).Find(&findUser)

	if findUser.ID != 0 {
		return nil, fmt.Errorf("账号已存在,请前往登录")
	}

	user := mysql.User{
		Account:  in.Account,
		Password: in.Password,
		Mobile:   in.Mobile,
	}

	mysql.DB.Debug().Create(&user)

	return &elem.RegisterResponse{
		Id: int64(user.ID),
	}, nil
}
